home *** CD-ROM | disk | FTP | other *** search
- Path: news.connect.net!usenet
- From: tomw@intelligraphics.com
- Newsgroups: comp.lang.c++
- Subject: Re: Mod or if?
- Date: 11 Feb 1996 22:56:05 GMT
- Organization: Connection Technologies
- Message-ID: <4fls65$a9m@dallas1.connect.net>
- References: <4fg3jm$fb8@gondor.sdsu.edu>
- Reply-To: tomw@intelligraphics.com
- NNTP-Posting-Host: igxtest.intelligraphics.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4fg3jm$fb8@gondor.sdsu.edu>, weikel@rohan.sdsu.edu (weikel) writes:
- >Which is better?
- >
- >x = (x mod 7);
- >
- >or
- >
- >if (x == 7) x = 0;
- >
- >in terms of performance? I know that the mod function is slow
- >,but the if method seems brute-forceish.
-
- My guess would be that the if statement is faster, in general. However,
- with P6 and P7 optimizations the *only* way to tell *for sure* is to time it.
- In this particular instance, an even faster method would probably be
-
- x = x & 7;
-
- This only works for values which are 2^n-1, of course. (I'm assuming
- that this code is in the middle of a loop that increments or decrements x.)
-
- Most people probably find the if statement to be a bit more readable than
- the modulus statement; that should also be taken into consideration. Only
- optimize if it's worth the effort and potential cost in readability.
-
- +---------------------------------------------------------------------------+
- + Tom Wheeler | Member NRA, NMRA +
- + tomw@intelligraphics.com | OS/2 user, C++ programmer +
- + ------------------------------------------------------------------------- +
- + Postmodernism is the refusal to think. Deconstructionism is the refusal +
- + to believe that anyone else can either. +
- +---------------------------------------------------------------------------+
- + Use or reproduction of this document or the author's email address for +
- + commercial purposes without the author's permission is prohibited. +
- +---------------------------------------------------------------------------+
-
-